#! /usr/bin
import sys
print(sys.argv)
print(sys.argv[0])

import os

def process_files(directory, output_dir):
    """
    遍历指定目录下的所有.txt文件，将内容转换为大写并保存到output_dir下。
    
    :param directory: 输入目录路径
    :param output_dir: 输出目录路径
    """
    # 确保输出目录存在
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)
    
    # 遍历目录
    for filename in os.listdir(directory):
        if filename.endswith(".txt"):
            input_path = os.path.join(directory, filename)
            with open(input_path, 'r', encoding='utf-8') as file:
                content = file.read().upper()
            
            # 保存到输出目录
            output_filename = f"upper_{filename}"
            output_path = os.path.join(output_dir, output_filename)
            with open(output_path, 'w', encoding='utf-8') as file:
                file.write(content)
                
            print(f"Processed {input_path} -> {output_path}")

# 使用示例
input_directory = './'
output_directory = './'
process_files(input_directory, output_directory)

def restore_files(directory):
    """
    遍历指定目录下的所有以'upper_'开头的文件，将内容转换为小写并保存为原始的.txt文件。
    
    :param directory: 输入/输出目录路径
    """
    for filename in os.listdir(directory):
        if filename.startswith("upper_") and filename.endswith(".txt"):
            input_path = os.path.join(directory, filename)
            with open(input_path, 'r', encoding='utf-8') as file:
                content = file.read().lower()
            
            # 保存为原始的.txt文件
            original_filename = filename[6:]  # 移除 "upper_" 前缀
            output_path = os.path.join(directory, original_filename)
            with open(output_path, 'w', encoding='utf-8') as file:
                file.write(content)
            
            # 删除以 "upper_" 开头的文件
            os.remove(input_path)
            
            print(f"Restored {input_path} -> {output_path}")

# 使用示例
directory = './'
restore_files(directory)






